#include "sequence.h"
#include "step.h"

Sequence::Sequence(QObject *parent)
    : m_pParent(parent)
{

}

Sequence& Sequence::operator= (Sequence& sequence){
    m_pParent = sequence.GetParent();

    QList<Step*> steps = sequence.GetSteps();
    for(int i = 0;i < steps.count();i++){
        Step* pStep = steps.at(i);
        Step* pNewStep = new Step(this);

        (*pNewStep) = (*pStep);

        m_Steps.append(pNewStep);
    }

    return *this;
}

bool Sequence::writeXML(QXmlStreamWriter& xmlWriter){
    // 写入当前操作的信息
    xmlWriter.writeStartElement("Sequence");

    if(m_Steps.size() > 0){
        xmlWriter.writeStartElement("Steps");
        foreach(Step* pStep,m_Steps){
            pStep->writeXML(xmlWriter);
        }
        xmlWriter.writeEndElement();  // 结束当前操作的元素
    }

    xmlWriter.writeEndElement();  // 结束当前操作的元素
    return true;
}

void Sequence::readFromXML(QXmlStreamReader& xmlReader){
    while (!(xmlReader.tokenType() == QXmlStreamReader::EndElement
             && xmlReader.name() == QStringLiteral("Sequence"))) {

        xmlReader.readNext();
        if(xmlReader.isStartElement()){
            QString className = xmlReader.name().toString();

            // 读取所有的序列
            if (xmlReader.isStartElement() && xmlReader.name() == QStringLiteral("Steps")) {
                while (!(xmlReader.tokenType() == QXmlStreamReader::EndElement
                         && xmlReader.name() == QStringLiteral("Steps"))) {
                    xmlReader.readNext();
                    if (xmlReader.isStartElement()) {
                        QString className = xmlReader.name().toString();
                        Step *pStep = (Step *) ObjectFactory::createObject(className.toLatin1(),this);
                        if (pStep != NULL) {
                            pStep->readFromXML(xmlReader);
                            this->AddStep(pStep);
                        }
                    }
                }
            }
        }
    }
}
